package com.spzx.user.controller;

import com.spzx.common.core.constant.Constants;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.user.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SmsController
 * @Author 波
 * @Date 2024/8/9 18:07
 */
@RestController
@RequestMapping("/sms")
@Tag(name = "短信发送接口")
public class SmsController extends BaseController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "发送短信")
    @GetMapping("/sendCode/{phoneNum}")
    public AjaxResult sendCode(@PathVariable("phoneNum") String phoneNum) {
       /* String redisCode = stringRedisTemplate.opsForValue().get(Constants.PHONE_IDENTIFYING_CODE + phoneNum);
        if(!StringUtils.isEmpty(redisCode)) {
            return success();
        }*/
        String identifyingCode = new DecimalFormat("0000").format(new Random().nextInt(10000));
        stringRedisTemplate.opsForValue().set(Constants.PHONE_IDENTIFYING_CODE + phoneNum, identifyingCode, Constants.PHONE_IDENTIFYING_CODE_TIME, TimeUnit.MINUTES);
        boolean flag=smsService.sendCode(phoneNum, identifyingCode);
        if(flag){
            return success();
        }else {
            return error("验证码发送失败！");
        }
    }
}
